package 第二类.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import 第二类.domain.Administrator;
import 第二类.domain.User;
import 第二类.service.AdministratorService;
import 第二类.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private AdministratorService adminitstratorService;    //注入管理员..

    @RequestMapping("/findAdministrator")   //管理员登录
    public String findAdministrator(Administrator administrator, String Vcode, HttpServletRequest request) throws IOException, ServletException {

        String checkCode=(String)request.getSession().getAttribute("checkCode_session");
        if(Vcode.equalsIgnoreCase(checkCode)){
            Administrator administrator1 = adminitstratorService.findAdministrator(administrator);
            request.getSession().setAttribute("admin",administrator1);
            if(request.getSession().getAttribute("admin")==null){
                request.setAttribute("login_error","用户名或者密码错误");
                return "/login.jsp";
            }else{
                return "/list.jsp";
            }
        }else{
            request.setAttribute("login_error","验证码错误");
            return "/login.jsp";
        }
    }

    @RequestMapping("/findAll")   //全部查询
    public ModelAndView findAll() throws IOException {
        List<User> all = userService.findAll();

        ModelAndView modelAndView=new ModelAndView("/list.jsp");
        modelAndView.addObject("allList",all);
        return modelAndView;
    }
    @RequestMapping("/findUser")   //条件查询
    public ModelAndView findUser(User user) throws IOException {
        List<User> userList = userService.findUser(user);

        ModelAndView modelAndView=new ModelAndView("/list.jsp");
        modelAndView.addObject("allList",userList);
        return modelAndView;
    }

    @RequestMapping("/findAdd")   //添加信息
    public String findAdd(User user) throws IOException {
        userService.findAdd(user);

        return "redirect:/user/findAll";
    }
    @RequestMapping("/SelectUser")   //条件查询
    public String SelectUser(User user) throws IOException {
        userService.SelectUser(user);

        return "redirect:/user/findAll";
    }
}
